package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.Tool;
import com.tms.domain.index.ToolStatusInfo;
import com.tms.mapper.develop.ToolMapper;
import com.tms.service.ToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模具实体业务实现
 *
 * @author Dicomsky
 */
@Service
public class ToolServiceImpl implements ToolService {

    @Autowired
    private ToolMapper toolMapper;

    @Override
    @Cacheable(cacheNames = "tools", key = "#id", unless = "#result == null")
    public Tool findToolById(int id) {
        return toolMapper.selectByPrimaryKey(id);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "toolList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allTools", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "toolStatusInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "scrapedTool", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "tools", key = "#result.id", unless = "#result == null || #result.id == 0")
            }
    )
    public Tool createTool(Tool tool) {
        toolMapper.insertSelective(tool);
        return tool;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "tools", key = "#tool.id"),
                    @CacheEvict(cacheNames = "toolList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allTools", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "toolStatusInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "scrapedTool", allEntries = true, beforeInvocation = true)
            }
    )
    public void deleteToolById(Tool tool) {
        toolMapper.deleteByPrimaryKey(tool.getId());
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "tools", key = "#tool.id", unless = "#result == null")
            },
            evict = {
                    @CacheEvict(cacheNames = "toolList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allTools", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "toolStatusInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "scrapedTool", allEntries = true, beforeInvocation = true)
            }
    )
    public Tool updateToolById(Tool tool) {
        int result = toolMapper.updateByPrimaryKeySelective(tool);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return tool;
    }

    @Override
    @Cacheable(cacheNames = "toolList", key = "#page + #pageSize + #code", unless = "#result == null")
    public PageInfo<Tool> findToolsByCode(int page, int pageSize, String code) {
        PageHelper.startPage(page, pageSize);
        List<Tool> list = toolMapper.selectByCode(code);
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(cacheNames = "allTools", key = "'findAll'", unless = "#result != 0")
    public int findAllTools() {
        return toolMapper.selectNumOfTool();
    }

    @Override
    @Cacheable(cacheNames = "scrapedTool", key = "'findAllScrap'", unless = "#result == 0")
    public int findAllScrap() {
        return toolMapper.selectNumOfToolScrap();
    }

    @Override
    @Cacheable(cacheNames = "toolStatusInfo", key = "'getToolStatusInfo'", unless = "#result == null")
    public List<ToolStatusInfo> getToolStatusInfo() {
        return toolMapper.selectStatusNum();
    }

    @Override
    public int getMaxSeqIdByCode(String code) {
        return toolMapper.selectMaxSeqIdByCode(code);
    }
}